package cappuccino.couldbook.ui;

import android.graphics.Bitmap;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cappuccino.couldbook.Constants;
import cappuccino.couldbook.R;
import cappuccino.couldbook.base.BaseActivity;
import cappuccino.couldbook.core.image.ImageReader;
import cappuccino.couldbook.core.mananger.AccountM;
import cappuccino.couldbook.data.PhoneNumber;
import cappuccino.couldbook.data.Result;
import cappuccino.couldbook.net.NetApi;
import cappuccino.couldbook.net.Network;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/9/5.
 */
public class AddnumberActivity extends BaseActivity implements View.OnClickListener {

    String TAG = "AddnumberActivity";

    @Override
    protected void init() {
        setContentView(R.layout.activity_addnumber);
    }


    TextInputLayout til_tag;
    TextInputLayout etNumber;
    Button btSubmit;
    ImageView imageView;

    @Override
    protected void bindView() {
        til_tag = (TextInputLayout) findViewById(R.id.til_addnumber_tag);
        etNumber = (TextInputLayout) findViewById(R.id.til_addnumber_number);
        btSubmit = (Button) findViewById(R.id.bt_addnumber_submit);
        btSubmit.setOnClickListener(this);
        imageView = (ImageView) findViewById(R.id.iv_addnumber_image);

        Log.v(TAG, "bindFinish");
        ImageReader.getReader(R.drawable.back).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(imageObserver);
        Log.v(TAG, "startRx");
    }

    Observer<Bitmap> imageObserver = new Observer<Bitmap>() {
        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(Bitmap bitmap) {
            Log.v(TAG, "onNext");
            imageView.setImageBitmap(bitmap);
            Log.v(TAG, "setImage finish");
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_addnumber_submit:
                submit();
                break;
            default:
                break;
        }
    }

    Observer<Result<PhoneNumber>> observer = new Observer<Result<PhoneNumber>>() {

        @Override
        public void onCompleted() {
            Log.v(TAG, "onCompleted");
        }

        @Override
        public void onError(Throwable throwable) {
            Log.v(TAG, "onError");
            Toast.makeText(AddnumberActivity.this.getApplicationContext(), "We meet some problem ", Toast.LENGTH_SHORT).show();
            Log.v(TAG, throwable.toString());
        }

        @Override
        public void onNext(Result<PhoneNumber> res) {
            Log.v(TAG, "OnNext");
            if (res.status == 1){
                Toast.makeText(AddnumberActivity.this.getApplicationContext(), "Add success", Toast.LENGTH_SHORT).show();
                finish();
            }
            else
                Toast.makeText(AddnumberActivity.this.getApplicationContext(), "Fail:" + res.message, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        showBack();
    }

    private void submit() {
        Log.v(TAG, "clicked");
        NetApi api = Network.getNetApi();
        String detail = til_tag.getEditText().getText().toString();
        String number = etNumber.getEditText().getText().toString();
        boolean info = true;
        if (detail == null || detail.isEmpty()) {
            info = false;
            til_tag.setError("标签不能为空");
        }
        if (number == null || number.isEmpty()) {
            info = false;
            til_tag.setError("号码不能为空");
        }
        if (info) {
                          AccountM accountM = new AccountM(this);
                int id = accountM.getAccout();
                if(id == -1)
                {
                    Toast.makeText(this,"请先登录",Toast.LENGTH_SHORT).show();
                }else{
                api.addnumber(id, detail, number).subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(observer);
            }
        }
    }

}
